package com.bootdo.system.filter;

import com.bootdo.common.utils.StringUtils;
import com.bootdo.sso.LoadUserInfoImp;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class CheckTokenFilter implements Filter {

    LoadUserInfoImp loadUserInfoImp;

    public CheckTokenFilter(LoadUserInfoImp loadUserInfoImp){
        System.err.println("CheckTokenFilter初始化...");
        this.loadUserInfoImp = loadUserInfoImp;
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }
    //127.0.0.1/wx/message?sso_token=49d03760-5945-44f6-a514-ae2ad5d0f3fb
    //127.0.0.1?sso_token=49d03760-5945-44f6-a514-ae2ad5d0f3fb
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        String ssoToken =  req.getParameter("sso_token");
        if(StringUtils.isNotEmpty(ssoToken)){
            loadUserInfoImp.checkToken(ssoToken);
        }
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
    }

}
